package com.founder.devwork.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.founder.devwork.dto.res.RepoAuthorStatsDTO;
import com.founder.devwork.dto.res.TopRepositoryDTO;
import com.founder.devwork.entity.Repository;
import com.founder.devwork.persistence.mapper.RepositoryMapper;
import com.founder.devwork.service.RepositoryService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * RepositoryServiceImpl 用于实现 RepositoryService 接口。
 * 提供查询仓库信息的方法。
 */
@Service
public class RepositoryServiceImpl extends ServiceImpl<RepositoryMapper, Repository> implements RepositoryService {

    @Autowired
    private RepositoryMapper repositoryMapper;

    /**
     * @inheritDoc
     */
    @Override
    public IPage<TopRepositoryDTO> queryRepositoryStats(Page<TopRepositoryDTO> page, String gitInstance, String name, String sortBy, String sortDir) {
        if (sortBy == null || sortBy.isEmpty()) {
            sortBy = "totalChanges";
        }
        if (sortDir == null || sortDir.isEmpty()) {
            sortDir = "DESC";
        }
        String effectiveSortBy;
        if ("commitCount".equals(sortBy)) {
            effectiveSortBy = "commitCount";
        } else if ("totalChanges".equals(sortBy)) {
            effectiveSortBy = "totalChanges";
        } else if ("allCommitCount".equals(sortBy)) {
            effectiveSortBy = "allCommitCount";
        } else if ("allTotalChanges".equals(sortBy)) {
            effectiveSortBy = "allTotalChanges";
        } else {
            effectiveSortBy = "totalChanges";
        }
        long total = repositoryMapper.countRepositoryStats(gitInstance, name);
        int offset = (int) ((page.getCurrent() - 1) * page.getSize());
        List<TopRepositoryDTO> list = repositoryMapper.queryRepositoryStats(gitInstance, name, effectiveSortBy, sortDir.toUpperCase(), offset, (int) page.getSize());
        page.setRecords(list);
        page.setTotal(total);
        return page;
    }

    @Override
    public List<RepoAuthorStatsDTO> getRepoAuthorsStats(String gitInstance, Long repoId, String sortBy, String sortDirection) {
        if (sortBy == null || sortBy.isEmpty()) {
            sortBy = "totalChanges";
        }
        if (sortDirection == null || sortDirection.isEmpty()) {
            sortDirection = "DESC";
        }
        String effectiveSortBy;
        if ("commitCount".equals(sortBy)) {
            effectiveSortBy = "commitCount";
        } else if ("totalChanges".equals(sortBy)) {
            effectiveSortBy = "totalChanges";
        } else if ("allCommitCount".equals(sortBy)) {
            effectiveSortBy = "allCommitCount";
        } else if ("allTotalChanges".equals(sortBy)) {
            effectiveSortBy = "allTotalChanges";
        } else {
            effectiveSortBy = "totalChanges";
        }
        return repositoryMapper.selectRepoAuthorsStats(gitInstance, repoId, effectiveSortBy, sortDirection.toUpperCase());
    }
}
